class Product < ActiveRecord::Base 
  validates_presence_of  :name, :price
  validates_uniqueness_of :name
  validates_numericality_of :price
  validates_presence_of :product_category_id, :restaurant_id
  
  belongs_to :restaurant
  belongs_to :product_category
  belongs_to :user
  has_many :product_line_items
  has_many :product_comments, :dependent => :destroy
  
  has_many :meal_product_memberships
  has_many :meals, :through => :meal_product_memberships
  
  before_destroy :destroy_empty_meals
  
  def destroy_empty_meals
    meals.each do |meal|
      Meal.destroy(meal) if meal.products.size==1
    end
  end
  
end
